﻿using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.Text;

namespace Acoris.Document
{
#if NETFX_CORE || WINDOWS_UWP
    using Windows.Security.Cryptography;
#elif NETFX_DESKTOP || WINDOWS_WPF
    using System.Security.Cryptography;
#endif

    /// <summary>
    /// 标识资源类型
    /// </summary>
    public enum ResourceType
    {
        /// <summary>
        /// 交互页(引用页等标识文本区内容的引用)
        /// </summary>
        Interaction,
        /// <summary>
        /// 标识本地视频
        /// </summary>
        Video,
        /// <summary>
        /// 标识本地音频
        /// </summary>
        Audio,
        /// <summary>
        /// 标识本地图片
        /// </summary>
        Image,
        /// <summary>
        /// 标识外部引用链接
        /// </summary>
        OutsideReference,
        /// <summary>
        /// 标识外部引用视频
        /// </summary>
        OutsideVideo,
        /// <summary>
        /// 标识外部引用图片
        /// </summary>
        OutsideImage,
    }

    /// <summary>
    /// 标识一个可交互资源
    /// </summary>
    public sealed class AcorisMarkdownResource:IEqualityComparer<AcorisMarkdownResource>
    {

        #region Fields

        string _resourceID;
        ResourceType _type;
        string _value;

        #endregion

        #region Constructor

        /// <summary>
        /// 初始化资源
        /// </summary>
        public AcorisMarkdownResource(ResourceType type)
        {
            _resourceID = RandomResourceIdGenerator.Gen();
            _type = type;
        }

        /// <summary>
        /// 加载已有的资源
        /// </summary>
        /// <param name="id"></param>
        public AcorisMarkdownResource(string id,ResourceType type)
        {
            _resourceID = id;
            _type = type;
        }

        public AcorisMarkdownResource (string id,string value, ResourceType type) : this(id,type)
        {
            Debug.Assert(value != null);            
            _value = value ?? throw new NullReferenceException();
        }

        #endregion

        /// <summary>
        /// 解决哈希冲突
        /// </summary>
        internal void GenNextResource()
        {
            _resourceID = RandomResourceIdGenerator.Gen();
        }

        #region Property & 属性

        /// <summary>
        /// 标识当前资源的标识符
        /// </summary>
        public string Identity {
            get {
                return _resourceID;
            }
        }
        
        /// <summary>
        /// 获取当前资源类型
        /// </summary>
        public ResourceType Type {
            get {
                return _type;
            }
        }

        /// <summary>
        /// 返回当前键的外部引用(只用当前资源类型为外部引用才有效)
        /// </summary>
        public string Value {
            get {
                return _value;
            }
            set {
                _value = value;
            }
        }

        /// <summary>
        /// 返回一个值，该值指示当前资源是否为外部引用资源
        /// </summary>
        public bool OutsideResource {
            get {
                return _type ==  ResourceType.OutsideImage      ||
                       _type ==  ResourceType.OutsideReference  ||
                       _type == ResourceType.OutsideVideo;
            }
        }

        #endregion

        #region Override

        public override string ToString()
        {
            return $"{_resourceID}";
        }

        public override bool Equals(object obj)
        {
            if(obj is AcorisMarkdownResource amr) {
                return amr._resourceID == this._resourceID;
            }
            return false;
        }

        public override int GetHashCode()
        {
            return this.GetHashCode() ^ _resourceID.GetHashCode();
        }

        #endregion  

        #region IEqualityComparer<AcorisMarkdownResource> Interface & 接口成员

        public bool Equals(AcorisMarkdownResource x, AcorisMarkdownResource y)
        {
            return x._resourceID == y._resourceID;
        }

        public int GetHashCode(AcorisMarkdownResource obj)
        {
            return obj.GetHashCode();
        }

        #endregion
    }
}
